package com.ruoyi.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.config.pay.CommonFieldName;
import com.ruoyi.common.config.pay.OrderIdGenerator;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.web.request.IESI123103Request;
import com.ruoyi.web.request.IESI123106Request;
import com.ruoyi.web.service.AgentPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.PaymentDetailMapper;
import com.ruoyi.web.domain.PaymentDetail;
import com.ruoyi.web.service.IPaymentDetailService;
import com.ruoyi.common.core.text.Convert;

/**
 * 结算账号明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-06
 */
@Service
public class PaymentDetailServiceImpl implements IPaymentDetailService 
{
    @Autowired
    private PaymentDetailMapper paymentDetailMapper;
    @Autowired
    private AgentPayService agentPayService;

    /**
     * 查询结算账号明细
     * 
     * @param sequenceNo 结算账号明细ID
     * @return 结算账号明细
     */
    @Override
    public PaymentDetail selectPaymentDetailById(String sequenceNo)
    {
        return paymentDetailMapper.selectPaymentDetailById(sequenceNo);
    }

    /**
     * 查询结算账号明细列表
     * 
     * @param paymentDetail 结算账号明细
     * @return 结算账号明细
     */
    @Override
    public List<PaymentDetail> selectPaymentDetailList(PaymentDetail paymentDetail) throws Exception
    {
        BusinessException.notNull(paymentDetail.getStartDate(),"请选择开始时间");
        BusinessException.notNull(paymentDetail.getEndDate(),"请选择结束时间");
        IESI123106Request request =new IESI123106Request();
        request.setStartDate(paymentDetail.getStartDate());
        request.setEndDate(paymentDetail.getEndDate());
        request.setAddInfo(paymentDetail.getAddInfo());
        HashMap<String, Object> resultMap= agentPayService.IESI123106(request,OrderIdGenerator.getDefault().nextId(),paymentDetail.getMerchCd());
        String respCode = (String) resultMap.get(CommonFieldName.RESP_CODE);
        String respMsg = (String) resultMap.get(CommonFieldName.RESP_MSG);
        List<PaymentDetail> list =new ArrayList<>();
        if ("00000".equals(respCode)) {
            Integer totalRecord=(Integer) resultMap.get("totalRecord");
            if (totalRecord>0){
              list=castList(resultMap.get("dataList"),PaymentDetail.class);
            }
        }else {
            BusinessException.notNull(null,respMsg);
        }

        return list;
    }
    public static <T> List<T> castList(Object obj, Class<T> clazz)
    {
        List<T> result = new ArrayList<T>();
        if(obj instanceof List<?>)
        {
            for (Object o : (List<?>) obj)
            {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }
    /**
     * 新增结算账号明细
     * 
     * @param paymentDetail 结算账号明细
     * @return 结果
     */
    @Override
    public int insertPaymentDetail(PaymentDetail paymentDetail)
    {
        return paymentDetailMapper.insertPaymentDetail(paymentDetail);
    }

    /**
     * 修改结算账号明细
     * 
     * @param paymentDetail 结算账号明细
     * @return 结果
     */
    @Override
    public int updatePaymentDetail(PaymentDetail paymentDetail)
    {
        return paymentDetailMapper.updatePaymentDetail(paymentDetail);
    }

    /**
     * 删除结算账号明细对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePaymentDetailByIds(String ids)
    {
        return paymentDetailMapper.deletePaymentDetailByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除结算账号明细信息
     * 
     * @param sequenceNo 结算账号明细ID
     * @return 结果
     */
    @Override
    public int deletePaymentDetailById(String sequenceNo)
    {
        return paymentDetailMapper.deletePaymentDetailById(sequenceNo);
    }
}
